<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class CategoryModel extends Model
{

	protected $_validate = array(
		array('name', 'require', '{%invalid_category_name}'),
	);


	/**
     * 取得上级分类选项(分级控制在<=3级)

     * @access public
     * @param integer $cateID 分类ID
     * @param integer $level 分类层级
     * @return string
     */
	public function getParentCategoryOptions($cateID=0, $level=3)
	{
		$field = array('category_id','name','level');
		$where = $order = $category = array();
		$order = array('path' => 'asc');
		$options = '';
		if ($cateID)
		{
			$category = $this->find($cateID);
			$where['level'] = array('lt', $category['level']);
			$result = $this->field($field)->where($where)->order($order)->select();
		}
		else
		{
			$category['parent'] = 0;
			$where['level'] = array('elt', $level);
			$result = $this->field($field)->where($where)->order($order)->select();
		}
		if ($result)
		{
			foreach ($result as $k => $v)
			{
				$selected = $category['parent'] == $v['category_id'] ? 'selected' : '';
				$cateName = str_repeat('&nbsp;&gt;&nbsp;', $v['level']-1).$v['name'];
				$options .= '<option value="'.$v['category_id'].'" '.$selected.'>'.$cateName.'</option>';
			}
		}
		return $options;
	}


	/**
     * 取得分类选项(分级控制在<=3级)

     * @access public
     * @param integer $cateID 当前分类ID
     * @param array $where 条件数组
     * @param array $field 查询字段
     * @param array $order 排序字段
     * @return string
     */
	public function getCategoryOptions($cateID=0, $where=array(), $field=array(), $order=array())
	{
		$options = '';
		$field = empty($field) ? array('category_id','name','level') : $field;
		$where = empty($where) ? array('level' => array('elt', 3)) : $where;
		$order = array('path' => 'asc');

		$result = $this->field($field)->where($where)->order($order)->select();
		if ($result)
		{
			foreach ($result as $k => $v)
			{
				$selected = $cateID == $v['category_id'] ? 'selected' : '';
				$cateName = str_repeat('&nbsp;&nbsp;&gt;&nbsp;', $v['level']-1).$v['name'];
				$options .= '<option value="'.$v['category_id'].'" '.$selected.'>'.$cateName.'</option>';
			}
		}
		return $options;
	}


	/**
     * 取得分类列表(<=3级)
     *
     * @access public
     * @param integer $level 分类层级
     * @return array
     */
	public function getCategoryList($level=3)
	{
		$field = array('category_id','name','level','display_order');
		$where = array('level' => array('elt', $level));
		$order = array('path' => 'asc');
		$list  = $this->where($where)->order($order)->select();
		return $list;
	}


	/**
	 * 更新分类下的问题计数
	 *
	 * @access public
	 * @param integer $cateID 分类ID
	 * @param string  $type('inc'=>递增,'dec'=>递减)
	 * @return null
	 */
	public function updateQuestionCount($cateID, $type='inc')
	{
		$categories = F('cache_categories');
		$where = 'category_id in ('. $categories[$cateID]['path'] .')';
		if ($type == 'inc')
		{
			$this->setInc('question_count', $where, 1);
		}
		else
		{
			$this->setDec('question_count', $where, 1);
		}
	}


	//删除分类
	function deleteCategory($cateID)
	{
	    $prefix = $this->tablePrefix;

	    //todo: 删除相关附件

	    //删除相关关注问题
		$sql = "Delete FROM {$prefix}starred_question WHERE question_id IN (SELECT question_id FROM {$prefix}question WHERE category_id='{$cateID}'))";
		$this->query($sql);

		//删除相关星号标记问题
		$sql = "Delete FROM {$prefix}watched_question WHERE question_id IN (SELECT question_id FROM {$prefix}question WHERE category_id='{$cateID}'))";
		$this->query($sql);

	    //删除相关提问
	    $Question = M('Question');
	    $Question->where('category_id='.$cateID)->delete();

	    //删除相关回答
		$Answer = M('Answer');
		$Answer->where('category_id='.$cateID)->delete();

		//删除相关评论
		$Comment = M('Comment');
		$Comment->where('category_id='.$cateID)->delete();

		//删除分类版主
		$Moderator = M('Moderator');
		$Moderator->where('category_id='.$cateID)->delete();

		//删除分类
		$this->delete($cateID);
	}


	//缓存分类
	public function cacheCategories()
	{
    	$results = $categories = array();
    	$results = $this->select();

    	if (!empty($results))
    	{
    		foreach ($results as $value)
    		{
    			$categories[$value['category_id']] = $value;
    		}
    		F('cache_categories', $categories);
    	}
	}

}

/* End of file CategoryModel.class.php */
/* Location: ./qqas/App/Lib/Model/CategoryModel.class.php */